package com.atguigu.srb.common.result;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: Result
 * Package: com.atguigu.srb.result
 * Description:
 *
 * @Author 王晓龙
 * @Create 2023/3/25 18:54
 * @Version 1.0
 */
@Data
public class Result {
    private Integer code;
    private String message;
    private Map<String, Object> data = new HashMap<>();

    private Result(){};

    //成功
    public static Result ok(){
        Result result = new Result();
        result.setCode(ResponseEnum.SUCCESS.getCode());
        result.setMessage(ResponseEnum.SUCCESS.getMessage());
        return result;
    }

    //失败
    public static Result error(){
        Result result = new Result();
        result.setCode(ResponseEnum.ERROR.getCode());
        result.setMessage(ResponseEnum.ERROR.getMessage());
        return result;
    }

    //其它情况
    public static Result setResult(ResponseEnum responseEnum){
        Result result = new Result();
        result.setCode(responseEnum.getCode());
        result.setMessage(responseEnum.getMessage());
        return result;
    }

    public Result code(Integer code){
        this.setCode(code);
        return this;
    }

    public Result message(String message){
        this.setMessage(message);
        return this;
    }

    public Result data(Map<String, Object> data){
        this.setData(data);
        return this;
    }

    public Result data(String key, Object value){
        this.data.put(key, value);
        return this;
    }
}